package service

import (
	"fmt"
	//"reflect"

	//	"initRoute"
	//	"controller"
	"net/http"
	"cmsapi/src/model"

	"github.com/gin-gonic/gin"

	"database/sql"
	//"time"
	_ "github.com/go-sql-driver/mysql"
	//	"gorm.io/gorm"
	//"gorm.io/driver/mysql"
)

//资讯列表
func ActivityIndex(c *gin.Context) {
	//model.ActivityQueryStruct
	query := model.ActivityQueryStruct{}

	err := c.BindQuery(&query)
	if err != nil {
		c.JSON(400, err.Error()) //{"error":"get error"}
	} else {
		//	c.String(http.StatusOK,"资讯列表 ")

		//db, err := sql.Open("mysql", "root:root@tcp(localhost:3306)/txgo")
		db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/txgo?charset=utf8")
		if err != nil {
			//	panic(err)
			defer db.Close()
		}
		// See "Important settings" section.
		//	db.SetConnMaxLifetime(time.Minute * 3)
		//db.SetMaxOpenConns(10)
		//db.SetMaxIdleConns(10)

		//	var id, title, username string
		//	db.QueryRow("SELECT id,title,username FROM tx_activity WHERE id=?", 1).Scan(&id, &title, &username)
		rows, _ := db.Query("SELECT id,title,username FROM tx_activity WHERE id=?", 1)

		for rows.Next() {
			var id int
			var title string
			rows.Scan(&id, &title)
			fmt.Println(id)
			fmt.Println(title)
		}

		c.JSON(http.StatusOK, rows)
	}
	//c.String(http.StatusOK,"ActivityList  =>%s",c.Query("type"))
	//
}

//资讯详细
func ActivityDetail(c *gin.Context) {
	//	c.String(http.StatusOK,"ActivityDetail  =>%s",c.Query("id"))
	c.JSON(http.StatusOK, model.ActivityDetail(101, "资讯标题"))
}

/*
//资讯详细2
func ActivityDetail2(c *gin.Context)  {

	dsn := "root:root@tcp(127.0.0.1:3306)/txgo?charset=utf8mb4&parseTime=True&loc=Local"
	db, _ := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	db.Raw("SELECT * FROM users WHERE id = ?", 1).Scan(&ActivityData)
	return ActivityData


		c.JSON(http.StatusOK,"abc")
}
*/
//资讯提交
func ActivitySave(c *gin.Context) {
	activityData := model.ActivityStruct{}
	err := c.BindJSON(&activityData)
	if err != nil {
		c.JSON(400, err.Error()) //{"error":"get error"}
	} else {
		//	c.String(http.StatusOK,"资讯列表 ")
		c.JSON(http.StatusOK, activityData)
	}
	//c.String(http.StatusOK,"ActivitySave  =>%s",c.Query("Save"))

}

//资讯删除
func ActivityDel(c *gin.Context) {
	c.String(http.StatusOK, "ActivityDel  =>%s", c.Query("Del"))
}
